﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Runtime.CompilerServices;
using UserControlLib.Enums;

namespace UserControlLib.Models
{
    [Serializable]
    public class DataModel : INotifyPropertyChanged
    {
        private string key = Guid.NewGuid().ToString();
        /// <summary>
        /// Key
        /// </summary>
        public string Key
        {
            get { return key; }
            set
            {
                this.key = value;
                OnPropertyChanged();
            }
        }

        private int order = 0;
        /// <summary>
        /// 序号
        /// </summary>
        public int Order
        {
            get { return order; }
            set
            {
                this.order = value;
                OnPropertyChanged();
            }
        }

        private bool isSelected = false;
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                this.isSelected = value;
                OnPropertyChanged();
            }
        }

        private bool changedEnabled = false;
        /// <summary>
        /// 修改使能
        /// </summary>
        public bool ChangedEnabled
        {
            get { return changedEnabled; }
            set
            {
                this.changedEnabled = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private List<string> errors = new List<string>();
        /// <summary>
        /// 错误提示集合
        /// </summary>
        public List<string> Errors
        {
            get { return errors; }
            set { errors = value; }
        }


        private string error = string.Empty;
        /// <summary>
        /// 错误提示,未使用
        /// </summary>
        public string Error
        {
            get { return error; }
            set { error = value; }
        }

        public string this[string columnName]
        {
            get
            {
                var vc = new ValidationContext(this, null, null);
                vc.MemberName = columnName;
                var res = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
                var result = this.GetType().GetProperty(columnName).GetValue(this, null);
                if (res.Count > 0)
                {
                    return string.Join(Environment.NewLine, res.Select(r => r.ErrorMessage).ToArray());
                }
                return string.Empty;
            }
        }

        public string GetHashKey()
        {
            return Guid.NewGuid().GetHashCode().ToString();
        }
    }

}
